import room3TestData from "../testData/testR3Data";
import room2TestData from "../testData/testR2Data";
import room1TestData from "../testData/testR1Data";
import room0TestData from "../testData/testR0Data";

var mfApi = require('../../api/api.js');

import bottomEnterTestData from "../testData/testBottomEnterData";

import TWEEN from './tween.js';

const app = getApp();

Page({

        /**
         * 页面的初始数据
         */
        data: {
                /**
                 * 当前房间index
                 */
                currentRoomIndex: app.globalData.gCurrentRoomIndex,
                baseUrl: app.globalData.baseUrl,

                /**
                 * 房间资源
                 */
                roomResourceData: null,
                localRoomData: null,

                picIndex: 1,
                picCurrentIndex: 0,

                /**
                 * 底部入口数据
                 */
                bottomEnterData: bottomEnterTestData,
                /**
                 * 0 未获取  1 已获取
                 */
                roomUserData: {},

                // 徽章tip
                isShowTipMask: false,
                isShowExitVideo: false,

                // 视频Tip
                isShowVideoTipPoster: false,
                videoTipImage: '',

                // 返回键
                top: 0,
                h: 0, //胶囊高度

                exitVideoUrl: '',
                exitVideoPosterUrl: ''
        },

        onShareTimeline:function(){
 
        },
        onVideoUpdate(e) {
            if (e.detail.currentTime > 0.1) {
              this.setData({
                picCurrentIndex: 0
              })
            }
          },
        /**
         * 生命周期函数--监听页面加载
         */
        onLoad: function (options) {
                this.setData({
                        currentRoomIndex: app.globalData.gCurrentRoomIndex,
                        top: app.menu.top,
                        h: app.menu.height
                })
                let temp = this.getLocalData();
                this.setData({
                        localRoomData: temp
                })

                this.scrollPosterView = this.selectComponent("#scroll-poster-view"); //组件的id
                // setTimeout(() => {
                //         this.getRoomInfoApi();                        
                // }, 1000);
                this.getRoomInfoApi();  
                
                // var intervalId = setInterval(function () {
                //     // console.log('11');
                //     TWEEN.update();
                //     // clearInterval(intervalId);
                //   }, 30);
        },

        onShow() {
                // setTimeout(() => {
                //         this.getRoomUserInfoApi();
                // }, 1000);
                this.getRoomUserInfoApi();
        },

        async getRoomUserInfoApi() {
                let res = await mfApi.http(
                        mfApi.getRoomList(app.globalData.openId), "GET", {});
                if (res.code != 200)
                        return;

                console.log("getRoomUserInfoApi   ===  ", res)
                let roomInfo = res.result.roomList.find(e => {
                        return e.roomNo == this.data.currentRoomIndex;
                })

                this.setData({
                        roomUserData: roomInfo
                })
                console.log("getRoomUserInfoApi   ===  ", this.data.roomUserData)

                let tempEnterData = this.data.bottomEnterData;
                tempEnterData.forEach(element => {
                        // element.index
                        let roomInfo = res.result.roomList.find(e => {
                                return e.roomNo == element.enterIndex;
                        });
                        if (roomInfo)
                                element.unlockFlag = roomInfo.unlockFlag;
                })

                this.setData({
                        bottomEnterData: tempEnterData
                })

        },

        async getRoomInfoApi() {
                let _self = this;
                let tempData = await mfApi.http(mfApi.getRoomInfo(this.data.currentRoomIndex, this.data.openId), "GET", {});
                console.log(tempData);
                if (tempData.code != 200) {
                        wx.showToast({
                                icon: "none",
                                title: '后端服务异常',
                        })
                        return;
                };

                var topArray = [];
                let videoData = tempData.result;
                videoData.forEach(element => {
                        let index = element.orderNo;
                        let tempLocal = _self.data.localRoomData.posterData.find(sub => {
                                return sub.id == index;
                        })
                        element.width = tempLocal.width;
                        element.height = tempLocal.height;
                        element.top = tempLocal.top;
                        element.left = tempLocal.left;
                        element.isHori = tempLocal.isHori;
                        topArray.push(parseFloat(element.top));
                });

                this.setData({
                        roomResourceData: videoData
                })
                console.log(videoData);
                

                this.scrollPosterView = this.selectComponent("#scroll-poster-view"); //组件的id

                // var coords = {
                //     x: 0,
                //   };
                //   new TWEEN.Tween(coords)
                //     .to(
                //       {
                //         x: 1.4,
                //       },
                //       5.6 * 1000
                //     )
                //     .yoyo(true)
                //     .repeat(Infinity)
                //     .easing(TWEEN.Easing.Linear.None)
                //     .onUpdate(function () {
                //         videoData[1].top = topArray[1] + coords.x + "%";
                //         videoData[3].top = topArray[3] + coords.x + "%";
                //         videoData[5].top = topArray[5] + coords.x + "%";
                //         videoData[6].top = topArray[6] + coords.x + "%";
        
                //         videoData[0].top = topArray[0] - coords.x + "%";
                //         videoData[2].top = topArray[2] - coords.x + "%";
                //         videoData[4].top = topArray[4] - coords.x + "%";
                //         _self.setData({
                //           roomResourceData: videoData
                //         })
                //     })
                //     .onComplete(function () {})
                //     .delay(0)
                //     .start();
        },

        getLocalData() {
                let temp = {};
                if (this.data.currentRoomIndex >= 0) {
                        if (this.data.currentRoomIndex == 0) {
                                temp = room0TestData;
                        } else if (this.data.currentRoomIndex == 1) {
                                temp = room0TestData;
                        } else if (this.data.currentRoomIndex == 2) {
                                temp = room1TestData;
                        } else if (this.data.currentRoomIndex == 3) {
                                temp = room2TestData;
                        } else if (this.data.currentRoomIndex == 4) {
                                temp = room3TestData;
                        } else if (this.data.currentRoomIndex == 5)
                                temp = room4TestData;
                }
                return temp;
        },

        /**
         * 视频海报点击
         */
        onPosterClick(e) {
                console.log(e.detail);
                if (e.detail.isHori) {
                        wx.navigateTo({
                                url: `/pages/videoSwiperHoriPage/videoSwiperHoriPage?selectId=` + e.detail.id,
                                // url: '/pages/videoSwiperPage/videoSwiperPage?selectId=' + e.detail,  isHori
                        })
                } else {
                        wx.navigateTo({
                                url: `/pages/videoSwiperPage/videoSwiperPage?selectId=` + e.detail.id,
                                // url: '/pages/videoSwiperPage/videoSwiperPage?selectId=' + e.detail,  isHori
                        })
                }

        },

        /**
         * 底部入口箭头点击
         */
        onBottomArrowClick(e) {
                console.log(e.detail);
                this.scrollPosterView.changePosterRootPosition(e.detail);
        },

        /**
         * stamp 点击
         */
        onStampIconClick() {
                let temp = this.data.isShowTipMask;
                this.setData({
                        isShowTipMask: !temp
                })
        },

        /**
         * 退出视频结束
         */
        onExitVideoEnded() {
                wx.navigateBack({
                        delta: 1,
                })
        },

        goBack() {
                var hour = new Date().getHours();
                let Back_Img_Type = 1;
                if (hour > 17 || hour < 6) {
                        Back_Img_Type = 3; //1是早上，2是中午，3是晚上
                } else if (hour > 5 && hour < 12) {
                        Back_Img_Type = 1;
                } else {
                        Back_Img_Type = 2;
                }
                this.setData({
                    picIndex:Back_Img_Type,
                    picCurrentIndex:this.data.currentRoomIndex
                });

                // 1-ROOM1.jpg
                let tempVideo = `${app.globalData.baseExitVideoUrl}/${Back_Img_Type}-ROOM${this.data.currentRoomIndex}.mp4`;
                let tempPoster = '';

                this.setData({
                        isShowExitVideo: true,
                        exitVideoUrl: tempVideo,
                        exitVideoPosterUrl: tempPoster
                })
        },

        onVideoTipCloseClick() {
                this.setData({
                        isShowVideoTipPoster: false,
                        videoTipImage: ''
                })
        },

        showVideoTip(e) {
                this.setData({
                        isShowVideoTipPoster: true,
                        videoTipImage: e.detail
                })
        },

        /**
         * 底部入口点击
         */
        onBottomEnterClick(e) {
                console.log(e.detail)
                app.globalData.indexTurnDoor = e.detail;
                this.goBack();
        },

        onShareAppMessage(object) {
                return {
                title: 'LA MER邀你一同参展，探寻属于你的自愈空间',
                path: `/pages/index/index?inChannel=commonShare`,
                imageUrl: "https://morefun-active-1.oss-cn-shanghai.aliyuncs.com/2022_lamer_cream_show/share/wx-share.jpg",
                }
        }
})